#ifndef RASTERTOFILE_H
#define RASTERTOFILE_H

#include <string>
#include <fstream>

#include <boost/regex.hpp> 
#include <boost/lexical_cast.hpp>

#include "..\AuroraTypes.hpp"
#include <borealis\AsciiRaster.hpp>

//2 params
//
class am_RasterToFile : public AuroraTypes::ModuleBase
{
public:
	//outfile
	//optional parameter is the date
	//a raster
	am_RasterToFile(std::string n)
	{
		name = n;
	}
	void run(AuroraTypes::StationList* stations, AuroraTypes::VariableList* variables, AuroraTypes::InVariables& inVars, AuroraTypes::OutVariables& outVars)
	{
		AuroraTypes::VariableList::const_accessor a;
		std::string file = outVars[0];
		std::string date;

		//optional date parameter
		if(inVars.size() == 2)
		{
			date = stations->at(0)->Now().Get<std::string>(inVars[1]);
		}

		variables->find(a,inVars[0]);

		if(a.empty())
			throw std::runtime_error("In " + std::string(__FILE__) +" @ line " + boost::lexical_cast<std::string>(__LINE__) +" - failed to get raster param");

		boost::shared_ptr<Borealis::AsciiRaster > raster = boost::dynamic_pointer_cast<Borealis::AsciiRaster>(boost::get<boost::shared_ptr<Borealis::VariableBase> >(a->second));


		std::ofstream out;

		date = boost::regex_replace(date, boost::regex(":"),"");
		file = date+std::string("-")+file;

		raster->SaveToFile(file);

	}
};

#endif